package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.math.BigDecimal;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class PointexChangeActivity extends SwipeBackActivity implements View.OnClickListener {
    private String TAG="PointexChangeActivity";
    private int pcCode;


    private LinearLayout linearLayout;
    private EditText editText;
    private TextView textView;
    private Button button;
    private String Tips;

    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pointex_change);
        linearLayout= (LinearLayout) findViewById(R.id.pc_back);
        linearLayout.setOnClickListener(this);
        editText= (EditText) findViewById(R.id.pc_number);
        textView= (TextView) findViewById(R.id.pc_show);
        button= (Button) findViewById(R.id.pc_button);
        button.setOnClickListener(this);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(editText.getText().toString().length()>1){
                    button.setEnabled(true);
                    button.setTextColor(Color.WHITE);
                    button.setBackgroundResource(R.drawable.begin_textview_shape_change);
                }else {
                    button.setEnabled(false);
                    button.setTextColor(Color.parseColor("#B8B8B8"));
                    button.setBackgroundResource(R.drawable.firist_edittext_shape);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        setStatusBar();
        intView();
    }

    private void intView() {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/point");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    pcCode = jsonObject.optInt("code", 0);
                    String data = jsonObject.optString("data", null);
                    if (!TextUtils.isEmpty(data)){
                        JSONObject jsonObjec = new JSONObject(data);
                        String  usable= jsonObjec.optString("usable", "");
                        BigDecimal bgg=new BigDecimal(usable);
                        textView.setText(bgg+"");
                    }
                 } catch (Exception e) {
                    e.printStackTrace();
                }
                if (pcCode == 200) {

                 }

                if (pcCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                } else {


                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "回调失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
             }
        });
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.pc_back:
                finish();
                break;
            case R.id.pc_button:
                dialog_show();
                break;
        }

    }



    private void dialog_show() {
        PointexChangeActivity.CustomPopup customPopup = new PointexChangeActivity.CustomPopup(this);
        new XPopup.Builder(this)
                .asCustom(customPopup)
                .show();
    }

    public class CustomPopup extends CenterPopupView {

        private TextView mytextView, banlance_resout;

        public CustomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.banlance_custom_popup;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            Tips=editText.getText().toString();
            Double d=Double.parseDouble(Tips);
            Double p=d/10;
            banlance_resout = findViewById(R.id.banlance_resout);
            banlance_resout.setText("积分兑换");
            mytextView = findViewById(R.id.balance_tishi);
            mytextView.setText("积分可兑换："+p+"元");
            findViewById(R.id.banlance_dialog_button).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String integral=editText.getText().toString();
                    PointexChange(integral);
                    dismiss();
                }
            });
        }

        protected void onShow() {
            super.onShow();
        }
    }

    @Override
    public void onResume() {
        intView();
        super.onResume();
    }


    private void PointexChange(String integral) {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/conversion");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        params.addBodyParameter("integral", integral);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    pcCode = jsonObject.optInt("code", 0);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(pcCode == 200){
                    String number=editText.getText().toString();
                    Intent intent = new Intent(PointexChangeActivity.this, ExchangeresultActivity.class);
                    intent.putExtra("number", number);
                    startActivity(intent);
                }
                if(pcCode == 301){
                    editText.setText("");
                    Toast.makeText(getBaseContext().getApplicationContext(), "兑换异常，请联系管理员!!!", Toast.LENGTH_SHORT).show();
                }
                if(pcCode == 201){
                    editText.setText("");
                    Toast.makeText(getBaseContext().getApplicationContext(), "积分不足", Toast.LENGTH_SHORT).show();
                }
                if(pcCode == 401){
                    editText.setText("");
                    Toast.makeText(getBaseContext().getApplicationContext(), "兑换失败", Toast.LENGTH_SHORT).show();

                }
                if(pcCode == 203){
                    editText.setText("");
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入要兑换的积分", Toast.LENGTH_SHORT).show();
                }
                if (pcCode == 500) {
                    editText.setText("");
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "回调失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
            }
        });

    }
}
